﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using MbUnit.Framework;
using System.Threading;
using HelloMVC.Test.TestHelper;

namespace HelloMVC.Test.Selenium
{
    [TestFixture]
    public class TaskBoardPage
    {
        private IWebDriver Browser { get; set; }

        [TestFixtureSetUp]
        public void SetUp()
        {
            Browser = new FirefoxDriver();
            Browser.Navigate().GoToUrl("http://localhost/NIRA_DEV/DashBoard/Agile.mvc/Taskboard");
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            Browser.Close();
        }

        [Test("Task Board Page: Verify task board column header text")]
        public void TestTaskBoardColumnHeader()
        {
            var colHeader = Browser.FindElement(By.Id("ag-step-head"));
            var colHeaderTexts = colHeader.FindElements(By.XPath("//div[contains(@class, 'ag-step-header')]/h3"));
            Assert.AreEqual(3, colHeaderTexts.Count);
            var colHeaderTextsExp = new[] { "To Do", "In Progress", "Done"};
            for (Int32 i = 0; i < colHeaderTextsExp.Length; i++)
            {
                Assert.IsTrue(colHeaderTexts[i].Text.StartsWith(colHeaderTextsExp[i]));
            }
        }

        [Test("Task Board Page: Parent tasks display")]
        public void TestTaskboardParentTaskDisplay()
        {
            var parentIssueKeys =
                Browser.FindElements(
                By.XPath("//div[@id='ag-step-families']/div[*]/div[contains(@class, 'ag-step-parent')]//div[contains(@class, 'ag-issue-key')]/a"));
            Assert.AreEqual(3, parentIssueKeys.Count);

            var expKeys = new[] {"PIM-1", "PIM-2", "PIM-3"};

            for (var i = 0; i < expKeys.Length; i++)
            {
                Assert.AreEqual(expKeys[i], parentIssueKeys[i].Text.Replace("\r\n", ""));
            }

        }

        [Test("Task Board Page: Sub Issues display, verify sub issues count")]
        public void TestSubIssueDisplay()
        {
            var parentIssues = new[] { "PIM-1", "PIM-2", "PIM-3" };
            foreach (var parentIssue in parentIssues)
            {
                var subTodoIssueKeys = Browser.FindElements(By.XPath(XPather.SubIssueKey(parentIssue, "0")));
                var subInproIssueKeys = Browser.FindElements(By.XPath(XPather.SubIssueKey(parentIssue, "1")));
                var subDoneIssueKeys = Browser.FindElements(By.XPath(XPather.SubIssueKey(parentIssue, "2")));
                Assert.AreEqual(1, subTodoIssueKeys.Count);
                Assert.AreEqual(1, subInproIssueKeys.Count);
                Assert.AreEqual(1, subDoneIssueKeys.Count);
            }
        }

        [Test("Task Board Page: other issues, verify other issues count")]
        public void TestProjectOtherIssuesDisplay()
        {
            var Todo = Browser.FindElements(By.XPath(XPather.OtherIssueKey("0")));
            var InPro = Browser.FindElements(By.XPath(XPather.OtherIssueKey("1")));
            var Done = Browser.FindElements(By.XPath(XPather.OtherIssueKey("2")));
            Assert.AreEqual(2, Todo.Count);
            Assert.AreEqual(2, InPro.Count);
            Assert.AreEqual(2, Done.Count);
        }

        [Test("Task Board Page: all projects dropdown list test, verify project count except self")]
        public void TestAllProjectsDropdownList()
        {
            var dropdownLink = Browser.FindElement(By.Id("projects-options"));
            dropdownLink.Click();
            Thread.Sleep(2000);
            var projectLinks = Browser.FindElements(By.XPath(XPather.DropdownListText("allprojects_dropdown", "*")));
            Assert.AreEqual(9, projectLinks.Count);
        }
    }
}
